﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EditUsers_Better
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        internal MainPresenter Presenter { get; set; }

        public void ShowTasks(ITaskService tasks)
        {
            int itemIndex = 0;
            foreach (var taskItem in tasks.Tasks)
            {
                // Add icon to the Imagelist
                imageListOptionIcons.Images.Add(taskItem.Value.Icon);
                // Add item to the ListView
                var task = new ListViewItem
                {
                    Text = taskItem.Key,
                    ImageIndex = itemIndex
                };
                //task. += (sender, args) => ExecuteTask(((Button)sender).Name);

                this.listViewOptions.Items.Add(task);
                itemIndex++;;
            }
        }

        private void ExecuteTask(string name)
        {
            Presenter.Execute(name);
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Presenter.CloseApp();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Create our Presenter which takes over control from now
            Presenter = new MainPresenter(this, TaskService.Instance);
        }

        private void menuItemGo_Click(object sender, EventArgs e)
        {
            // Ask the presenter to perform the action associated with this item
            ExecuteTask(listViewOptions.Items[listViewOptions.SelectedIndices[0]].Text);
        }

        private void listViewOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewOptions.SelectedIndices.Count > 0)
            {
                menuItemGo.Enabled = true;
            }
            else
            {
                menuItemGo.Enabled = false;
            }
        }

    }
}